package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    /**
     * 新增套餐，同时需要保存套餐和菜品的关联关系
     * @param setmealDTO
     */
     @Transactional
    public void saveWithDish(SetmealDTO setmealDTO) {
        Setmeal setmeal=new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
//        像套餐表插入数据
        setmealMapper.insert(setmeal);
//        获取插入的套餐数据的id
        Long setmealId = setmeal.getId();

        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(setmealId);
        });
        setmealDishMapper.insertBatch(setmealDishes);

    }

    /*
     * @Description  套餐分页
     * @Param [setmealPageQueryDTO]
     * @return com.sky.result.PageResult
     **/
    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> page=setmealMapper.pageQuery(setmealPageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }

    /*
     * @Description  批量删除
     * @Param [ids]
     * @return void
     **/
    @Override
    public void deleteBatch(List<Long> ids) {
//     判断套餐是否在售卖
        for (Long id : ids) {
        Setmeal setmeal=setmealMapper.getById(id);
         if (setmeal.getStatus()== StatusConstant.ENABLE){
             throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
         }
        }
//        删除套餐
        setmealMapper.deleteByIds(ids);
//       删除套餐和菜品的关联
        setmealDishMapper.deleteBySetmealIds(ids);
    }

    /*
     * @Description  查询套餐和菜品的联系
     * @Param [id]
     * @return com.sky.vo.SetmealVO
     **/
    @Override
    public SetmealVO getByIdWithDish(Long id) {
//        获取套餐
        Setmeal setmeal = setmealMapper.getById(id);
        SetmealVO setmealVO=new SetmealVO();
        BeanUtils.copyProperties(setmeal,setmealVO);
       List<SetmealDish> setmealDishes=setmealDishMapper.getsetmealDishBySetmealId(id);
        setmealVO.setSetmealDishes(setmealDishes);
        return setmealVO;
    }

    /*
     * @Description  修改套餐信息
     * @Param [setmealDTO]
     * @return void
     **/
    @Override
    public void update(SetmealDTO setmealDTO) {
        Setmeal setmeal=new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
//        更新套餐信息
        setmealMapper.update(setmeal);
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
//            删除与套餐关联的旧菜品
            setmealDishMapper.deleteBySetmealId(setmealDTO.getId());
        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(setmealDTO.getId());
        });
//        插入新 菜品
        setmealDishMapper.insertBatch(setmealDishes);
    }

    /*
     * @Description  修改套餐状态
     * @Param [setmeal]
     * @return void
     **/
    @Override
    public void updateStatus(Setmeal setmeal) {
        setmealMapper.update(setmeal);
    }

    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}
